import turtle

# 设置画布大小
turtle.setup(800, 800)  # 设定一个较小的画布（800x800）

# 四个鳄鱼的初始位置（正方形的四个角）
positionLion1 = (-300.00, 300.00)  # 左上角
positionLion2 = (300.00, 300.00)   # 右上角
positionLion3 = (-300.00, -300.00) # 左下角
positionLion4 = (300.00, -300.00)  # 右下角

# 计算正方形中心点的位置
centerX = (positionLion1[0] + positionLion2[0] + positionLion3[0] + positionLion4[0]) / 4
centerY = (positionLion1[1] + positionLion2[1] + positionLion3[1] + positionLion4[1]) / 4
positionHuman = (centerX, centerY)  # 人的位置在正方形中心

escapeDegree = 240

# 设置画笔样式
turtle.pensize(2)

# 每次循环模拟时间流逝
for x in range(1000):  # 可适当增大范围
    turtle.color("black")
    turtle.penup()
    turtle.goto(positionHuman)
    turtle.dot(2, "brown")  # 人的位置
    turtle.pendown()
    turtle.setheading(escapeDegree)

    # 计算与每个鳄鱼的距离
    lenthLion1 = turtle.distance(positionLion1)
    lenthLion2 = turtle.distance(positionLion2)
    lenthLion3 = turtle.distance(positionLion3)
    lenthLion4 = turtle.distance(positionLion4)

    if (x >= 2):  # 判断是否需要调整方向
        # 选择距离最远的鳄鱼来逃跑
        distances = [lenthLion1, lenthLion2, lenthLion3, lenthLion4]
        max_distance_index = distances.index(max(distances))

        if max_distance_index == 0:  # 向鳄鱼1逃跑
            escapeDegree = escapeDegree - 10
            turtle.fd(5)  # 减小每步移动距离以加快速度
        elif max_distance_index == 1:  # 向鳄鱼2逃跑
            escapeDegree = escapeDegree + 10
            turtle.fd(5)
        elif max_distance_index == 2:  # 向鳄鱼3逃跑
            escapeDegree = escapeDegree - 10
            turtle.fd(5)
        else:  # 向鳄鱼4逃跑
            escapeDegree = escapeDegree + 10
            turtle.fd(5)
    else:
        turtle.fd(5)  # 第一次运行时正常移动
        print("初始状态", escapeDegree)

    positionHuman = turtle.position()

    # 鳄鱼1的运动
    turtle.color("green")
    turtle.penup()
    turtle.goto(positionLion1)
    turtle.dot(2, "green")
    turtle.pendown()
    positionLion1ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionLion1ToHuman)
    turtle.fd(10)  # 鳄鱼1的速度为10米每次
    positionLion1 = turtle.position()

    # 鳄鱼2的运动
    turtle.color("red")
    turtle.penup()
    turtle.goto(positionLion2)
    turtle.dot(2, "red")
    turtle.pendown()
    positionLion2ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionLion2ToHuman)
    turtle.fd(10)  # 鳄鱼2的速度为10米每次
    positionLion2 = turtle.position()

    # 鳄鱼3的运动
    turtle.color("blue")
    turtle.penup()
    turtle.goto(positionLion3)
    turtle.dot(2, "blue")
    turtle.pendown()
    positionLion3ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionLion3ToHuman)
    turtle.fd(10)  # 鳄鱼3的速度为10米每次
    positionLion3 = turtle.position()

    # 鳄鱼4的运动
    turtle.color("yellow")
    turtle.penup()
    turtle.goto(positionLion4)
    turtle.dot(2, "yellow")
    turtle.pendown()
    positionLion4ToHuman = turtle.towards(positionHuman)
    turtle.setheading(positionLion4ToHuman)
    turtle.fd(8)  # 鳄鱼4的速度为8米每次
    positionLion4 = turtle.position()

    # 结束条件：如果某只鳄鱼的距离变得小于人的速度，代表人会被抓住
    if lenthLion1 <= 10 or lenthLion2 <= 10 or lenthLion3 <= 10 or lenthLion4 <= 10:
        print(f"人被捕捉，时间为 {x*0.1} 秒")
        break

# 关闭画布窗口
turtle.done()
